﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;


namespace Amarok.Presentation
{
	/// <summary>
	/// </summary>
	internal static class ViewInspector
	{
		// constants
		private static readonly HandlerInspector[] sInspectors = new HandlerInspector[] {
			new ViewActionHandlerInspector()
		};

		// cached handler information
		private static readonly HandlerInfoDictionary sCache = new HandlerInfoDictionary();


		#region ++ Public Interface ++

		/// <summary>
		/// </summary>
		/// <param name="targetType"></param>
		public static HandlerInfo[] Inspect(Type targetType)
		{
			Verify.NotNull(targetType, "targetType");

			return sCache.GetOrAdd(
				targetType,
				type => _GetHandlerInfos(type));
		}

		#endregion

		#region Implementation

		private static HandlerInfo[] _GetHandlerInfos(Type type)
		{
			return _EnumerateHandlerInfos(type).ToArray();
		}

		private static IEnumerable<HandlerInfo> _EnumerateHandlerInfos(Type type)
		{
			while (type != null)
			{
				foreach (var handlerInfo in _EnumerateHandlerForSingleType(type))
					yield return handlerInfo;

				type = type.BaseType;
			}
		}

		private static IEnumerable<HandlerInfo> _EnumerateHandlerForSingleType(Type type)
		{
			var bindingFlags =
				BindingFlags.Instance |
				BindingFlags.Static |
				BindingFlags.NonPublic |
				BindingFlags.Public |
				BindingFlags.DeclaredOnly;

			var methods = type.GetMethods(bindingFlags);

			for (Int32 m = 0; m < methods.Length; m++)
			{
				if (methods[m].IsAbstract)
					continue;

				var handlerInfo = _Inspect(methods[m]);

				if (handlerInfo != null)
					yield return handlerInfo;
			}
		}

		private static HandlerInfo _Inspect(MethodInfo method)
		{
			for (Int32 i = 0; i < sInspectors.Length; i++)
			{
				var handlerInfo = sInspectors[i].Inspect(method);

				if (handlerInfo != null)
					return handlerInfo;
			}

			return null;
		}

		#endregion

		#region Implementation (Nested Types)

		private sealed class HandlerInfoDictionary : ConcurrentDictionary<Type, HandlerInfo[]>
		{
		}

		#endregion

	}
}
